# -*- coding: utf-8 -*-
__author__ = 'Paul'
import random
from zope import schema
from zope.formlib import form
from zope.interface import implements
from zope.component import getMultiAdapter
from plone.app.portlets.portlets import base
from plone.memoize.instance import memoize
from plone.portlets.interfaces import IPortletDataProvider
from DateTime import DateTime
from Products.Five.browser.pagetemplatefile import\
    ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName
from Products.bpContents import _
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

from plone.memoize.compress import xhtml_compress
from Products.bpContents.articulo import IArticulo
from Products.bpContents.galeria import IGaleria
from Products.bpContents.evento import IEvento
from zope.schema.interfaces import IVocabularyFactory

from zope.site.hooks import getSite
import unicodedata
from intereses_portlets import CanalesVocabularyFactory
from Acquisition import aq_parent,aq_base,aq_inner

from OFS.interfaces import IOrderedContainer

class IDeptoServicios(IPortletDataProvider):
    """Interface de marca para el Portle de servicios """
    count = schema.Int(title=_(u'Number of items to display'),
        description=_(u'How many items to list.'),
        required=True,
        default=5)

class Assignment(base.Assignment):
    implements(IDeptoServicios)
    def __init__(self, count=5, randomize=False, sitewide=False):
        self.count = count


    @property
    def title(self):

        title = _(u"Listado de servicios")

        return title


class Renderer(base.Renderer):
    ##_template = ViewPageTemplateFile('intereses_portlet.pt')
    render = ViewPageTemplateFile('deptoServicios.pt')



    def __init__(self, *args):
        base.Renderer.__init__(self, *args)
        self.idDestacado=""
        self.idCanal=""
        self.canalUrl=""
        self.context = aq_inner(self.context)
        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        self.anonymous = portal_state.anonymous()
        self.portal_url = portal_state.portal_url()
        self.typesToShow = portal_state.friendly_types()

        plone_tools = getMultiAdapter((self.context, self.request), name=u'plone_tools')
        self.catalog = plone_tools.catalog()
        self.portal = getMultiAdapter((self.context, self.request),
            name=u'plone_portal_state').portal()

    @property
    def available(self):
        return True
        items=self._data()
        if len(items)>0:
            return True
        else:
            return None

    def getServicios(self):
        """devuelve el brain de la busqueda de serviciso"""
        items = self._data()
        if len(items)>0:
            return items
        else:
            return None


    def dameTitulo(self):
        strR='Servicios que se prestan in situ:'

        if self.context.portal_type=="Products.bpContents.servicio":
            strR='Otros servicios de %s' % aq_parent(aq_inner(self.context)).title

        return strR



    @memoize
    def _data(self):
        """ devueklve los servicios
        """
        from Products.bpContents.servicio import IServicio
        limit = self.data.count+1
        rutaF='/'.join(self.context.getPhysicalPath())
        if self.context.portal_type=="Products.bpContents.servicio":
            rutaF='/'.join(aq_parent(aq_inner(self.context)).getPhysicalPath())


        servicios=self.catalog(object_provides=IServicio.__identifier__,
            path={ "query": rutaF },
            sort_order='reverse')[:limit]

        return servicios

class AddForm(base.AddForm):
    form_fields = form.Fields(IDeptoServicios)
    label = _(u"Add Recent Portlet")
    description = _(u"This portlet displays recently modified content.")

    def create(self, data):
        return Assignment(count=data.get('count', 3),)

class EditForm(base.EditForm):
    form_fields = form.Fields(IDeptoServicios)
    label = _(u"Edit Recent Portlet")
    description = _(u"This portlet displays recently modified content.")